var doumee = (function(dm){
	'use strict';
	
	window.onload = function(){
		//获取输入框焦点
		$('input[type="text"],input[type="tel"],input[type="password"],textarea').on('focus', function(){
			var target = this;
			setTimeout(function(){
				target.scrollIntoViewIfNeeded();
			},400);
		});
	};
	
	//初始化页面滑动组件
	dm.init = function (obj,options) {
		
		var options = options == undefined ? options = {} : options = options ;
		//上拉加载，默认禁用
		options.down == undefined ? options.down = false : options.down ;
		//下拉刷新，默认禁用
		options.up == undefined ? options.up = false : options.up ;
		//启用满屏，默认高度由内容决定
		options.full == undefined ? options.full = false : options.full ;
		//禁用弹性滑动，默认关闭禁用
		options.free == undefined ? options.free = false : options.free ;
		//启用滚动条，默认关闭
		options.scroll == undefined ? options.scroll = false : options.scroll ;
		
		if(options.full){
			$(obj).find('.content').css('min-height',$(obj).height());
		};
		
		var refreshEnd = false;
		
		var swiper = new Swiper(obj,{
			direction: 'vertical',//vertical：竖向切换,horizontal: 横向切换
			loop: false,
			slidesPerView: 'auto',
			setWrapperSize :true,
			mousewheel: true,
			watchOverflow:options.free,

			freeMode:true,
			passiveListeners : false,
			
			//如果需要滚动条
			scrollbar: {
			  el: options.scroll ? obj+' .swiper-scrollbar' : '',
			  hide: true,//自动隐藏
			},
			
			preloadImages:true,//默认为true，Swiper会强制加载所有图片。
			updateOnImagesReady : true,//当所有的内嵌图像（img标签）加载完成后Swiper会重新初始化
			
			observer:true,//当改变swiper的样式（例如隐藏/显示）或者修改swiper的子元素时，自动初始化swiper。
			observeParents:true,//当Swiper的父元素变化，自动初始化swiper。
			
			on:{
				init:function(){
					//添加滚动条
					if(options.scroll){
						var scroll = document.createElement('div');
						scroll.className ='swiper-scrollbar';
						this.el.appendChild(scroll);
					};
					//添加刷新容器
					if(options.up){
						var fresh = document.createElement('div');
						fresh.className = 'pull-up';
						fresh.innerHTML = '释放刷新';
						this.wrapperEl.appendChild(fresh);
					};
					//添加加载容器
					if(options.down){
						var load = document.createElement('div');
						load.className = 'pull-down';
						this.wrapperEl.appendChild(load);
					};
				},
				touchStart:function(){
					if(options.up){
						var refresh = this.$el.find('.pull-up');
						if(!refreshEnd){
							refresh.text('释放刷新');
						};
					};
					//如果页面没有一屏，不展示底部文字
					if(options.down && this.height >= this.slides[0].clientHeight){
						this.$el.find('.pull-down').text('');
					}else{
						if(this.$el.find('.pull-down').text() == ''){
							this.$el.find('.pull-down').text('拼命加载中');
						};
					};
				},
				touchEnd:function(){
					if(options.up){
						var refresh = this.$el.find('.pull-up');
						swiper = this;
						if(this.translate > 50){
							swiper.setTransition(300);
							swiper.setTranslate(50);
							swiper.touchEventsData.isTouched=false;//跳过touchEnd事件后面的跳转
							swiper.allowTouchMove = false;
							refresh.text('正在刷新…');
							refreshEnd = true;
							setTimeout(function(){
								swiper.allowTouchMove = true;
								refresh.text('刷新完成');
								if(typeof options.up == 'function'){
									options.up(refresh);
								};
								refreshEnd = false;
							},1000);
						};
					};
				},
				momentumBounce:function(){
					if(options.down){
						swiper = this ;
						var reload = swiper.$el.find('.pull-down');
						if(swiper.translate < -100){
							swiper.allowTouchMove = false;
							swiper.params.virtualTranslate = true;//定住不给回弹
							setTimeout(function(){
								swiper.params.virtualTranslate = false;
								swiper.allowTouchMove = true;
								if(typeof options.down == 'function'){
									options.down(reload);
								};
							},1000);
						};
					};
				},
			}
		});
		return swiper;
	};
	
	
	//打开子页面
	window.openSubMain = function(obj){
		if(typeof obj == 'string'){
			$(obj).css("transform",'translate3d(0,0,0)');
		}else if(typeof obj == 'object'){
			obj.css("transform",'translate3d(0,0,0)');
		}else{
			$('.subMain').css("transform",'translate3d(0,0,0)');
		};
	};
	
	//关闭子页面
	window.closeSubMain = function(obj){
		if(typeof obj == 'string'){
			$(obj).css("transform",'translate3d(100%,0,0)');
		}else if(typeof obj == 'object'){
			obj.css("transform",'translate3d(100%,0,0)');
		}else{
			$('.subMain').css("transform",'translate3d(100%,0,0)');
		};
	};
	
	//获取url中的参数
	dm.getParam = function(name){
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
		var r = window.location.search.substr(1).match(reg);  //匹配目标参数
		if (r != null) return decodeURI(r[2]); return null; //返回参数值
	};
	
	//等比例控制图片
	dm.imgRate = function(){
		//获取全部带有比例的属性
		var target = $('body').find('*[data-rate]');
		target.each(function(){
			var node = $(this);
			var nodeWidth = node.width();
			console.log(nodeWidth)
			var rate = node.attr('data-rate');
			var reg = /\//g;
			if(rate){
				if(reg.test(rate)){
					//判断是否为分数
					var arr = rate.split("/");
					rate = parseInt(arr[0])/parseInt(arr[1]);
				};
				node.height(nodeWidth/rate);
			};
		});
		
		$(window).resize(function(){
			dm.imgRate();
		});
	};
	
	//创建banner
	dm.banner = function(obj,options){
		
		var options = options == undefined ? options = {} : options = options ;
		options.loop == undefined ? options.loop = true : options.loop = false ;
		options.direct == undefined ? options.direct = 'horizontal' : options.direct;//横向：horizontal，纵向：vertical
		
		var swiper = new Swiper(obj,{
			loop: options.loop,//循环
			autoplay:{
				disableOnInteraction: false,//切换后还会继续自动轮播
			},//自动轮播
			watchOverflow:true,//slide只有1个且loop为false时,swiper会失效且隐藏导航等。
			direction:options.direct,
			
			preloadImages:false,//默认为true，Swiper会强制加载所有图片。
			updateOnImagesReady : true,//当所有的内嵌图像（img标签）加载完成后Swiper会重新初始化
			
			pagination: {
			  el: obj+' .swiper-pagination',
			  clickable :true,
			},
		});
		return swiper;
	};
	
	//建立一個可存取到该file的图片url
	dm.file = function(file) {
	    var url = null ;
	    // 下面函数执行的效果是一样的，只是需要针对不同的浏览器执行不同的 js 函数而已
	    if (window.createObjectURL!=undefined) { // basic
	        url = window.createObjectURL(file) ;
	    } else if (window.URL!=undefined) { // mozilla(firefox)
	        url = window.URL.createObjectURL(file) ;
	    } else if (window.webkitURL!=undefined) { // webkit or chrome
	        url = window.webkitURL.createObjectURL(file) ;
	    };
	    return url ;
	};
	
	return dm;
	
})(window.doumee || {});